package com.otaku.controller;

import com.otaku.model.entity.CandyBlog;
import com.otaku.service.CandyBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：ganxiang <br>
 * 时间：2018年04月20日 13:14<br>
 * 标题：CandyBlogController<br>
 * 功能：微博（暂定）controller<br>
 */
@RestController
@RequestMapping("/candyBlog")
public class CandyBlogController implements BaseController{

    @Autowired
    private CandyBlogService candyBlogService;

    @PostMapping("/create")
    public ResponseEntity<?> create(@RequestBody @Valid CandyBlog candyBlog) {
        candyBlogService.create(candyBlog);
        return ResponseEntity.ok().body(ok("新增微博成功"));
    }

    @GetMapping("/detail")
    public ResponseEntity<?> detail(@NotEmpty(message = "id不能为空") Long id) {
        return ResponseEntity.ok().body(candyBlogService.findById(id));
    }

    @GetMapping("/query")
    public ResponseEntity<?> query() {
        Map<String, Object> result = new HashMap<>();
        List<CandyBlog> candyBlogs = candyBlogService.query();
        result.put("isSuccess", true);
        result.put("items", candyBlogs);
        return ResponseEntity.ok().body(result);
    }

    @PostMapping("/update")
    public ResponseEntity<?> update(@RequestBody @Valid CandyBlog candyBlog) {
        candyBlogService.update(candyBlog);
        return ResponseEntity.ok().body(ok("修改微博成功"));
    }
}
